package com.itheima.edu.info.manager.dao;

import com.itheima.edu.info.manager.domain.Student;

import java.util.Comparator;
import java.util.TreeSet;

/*
库管类:
核心职责管理我们的数据仓库--数组或集合
这里的所有方法和功能都是给业务层用
 */
public class OtherStudentDao implements BaseStudentDao {
   // private static ArrayList<Student> stu = new ArrayList<>();

    private  static  TreeSet<Student> stu=new TreeSet<>(new Comparator<Student>() {
        @Override
        public int compare(Student o1, Student o2) {
            int i = Integer.parseInt(o2.getAge()) - Integer.parseInt(o1.getAge());
            int j = i == 0 ? o1.getName().compareTo(o2.getName()) : i;
            if(o1.getSid().compareTo(o2.getSid())==0){
                return 0;
            }else {
                return j;

            }
        }
    });

    static {
        Student stu1 = new Student("heima001", "张三", "23", "1997-10-10");
        Student stu2 = new Student("heima002", "李四", "24", "1996-10-10");
        stu.add(stu1);
        stu.add(stu2);
    }
    public boolean addStudent(Student student) {
        stu.add(student);
        return true;
    }

  /*  public Student[] findAllStudent() {
        Student[] students = new Student[stu.size()];
        for (int i = 0; i < stu.size(); i++) {
            Student student = stu.get(i);
            students[i] = student;
        }
        return students;
    }*/

    public Student[] findAllStudent() {
        Student[] stus = new Student[stu.size()];
        Student[] students = stu.toArray(stus);
        return students;
    }

    public void deleteStudent(String delId) {
        int index = getIndex(delId);
        Student []stus=new Student[stu.size()];
        Student[] students = stu.toArray(stus);
        for (int i = 0; i < students.length; i++) {
            if (students[index].getSid().equals(students[i].getSid())){
              stu.remove(students[index]);
            }
        }
       // stu.remove();
    }

   /* public int getIndex(String id) {
        int index = -1;
        for (int i = 0; i < stu.size(); i++) {
            Student student = stu.get(i);
            if (student != null && student.getSid().equals(id)) {
                index = i;
            }
        }
        return index;
    }*/
   public int getIndex(String id) {
       int index = -1;
       Student []stus=new Student[stu.size()];
       Student[] students = stu.toArray(stus);
       for (int i = 0; i < students.length ;i++) {
           if (students != null && students[i].getSid().equals(id)) {
               index = i;
           }
       }
       return index;
   }

   /* public void updateStudent(String updateId, Student newStu) {

        int index = getIndex(updateId);
        stu.set(index, newStu);
    }*/
   public void updateStudent(String updateId, Student newStu) {
       Student []stus=new Student[stu.size()];
       Student[] students = stu.toArray(stus);
       int index = getIndex(updateId);
       for (int i = 0; i < students.length; i++) {
           if (updateId.equals(students[i].getSid())&&students!=null){
               students[index]=newStu;
               deleteStudent(students[i].getSid());
           }
       }
       for (int i = 0; i < students.length; i++) {
           stu.add(students[i]);
       }


   }
}
